package com.tbit.uqbike.webmanager.controller;

import cn.hutool.core.date.DateUtil;
import com.tbit.uqbike.object.pojo.vo.OperationReportVO;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.OperationReportService;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtil;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 运营统计-运营报表
 *
 * @Author:BUGTian
 * @DATE: 2022/11/10
 */
@Slf4j
@RestController
@RequestMapping("/operationReport")
public class OperationReportController {

    @Autowired
    private OperationReportService operationReportService;


    /**
     * 运营报表按区域汇总
     *
     * @param adAccountIds 区域id 多个用,隔开
     * @param startTime    开始时间
     * @param endTime      结束时间
     * @return
     */
    @GetMapping("/groupByArea")
    public Result<List<OperationReportVO>> groupByArea(String adAccountIds, Date startTime, Date endTime) {
        List<OperationReportVO> list = operationReportService.groupByArea(adAccountIds, startTime, endTime);
        return Result.success(list);
    }

    /**
     * 运营报表按区域汇总导出
     *
     * @param adAccountIds 区域id 多个用,隔开
     * @param startTime    开始时间
     * @param endTime      结束时间
     * @return
     */
    @GetMapping("/groupByAreaExport")
    public void groupByAreaExport(String adAccountIds, Date startTime, Date endTime, HttpServletResponse response) {
        List<OperationReportVO> list = operationReportService.groupByArea(adAccountIds, startTime, endTime);
        LinkedHashMap<String, String> titleMap = new LinkedHashMap<>();
        String format = DateUtil.format(endTime, "(MM月dd日)");
        titleMap.put("areaName", "运营区域");
        titleMap.put("vehicleTotal", "车辆总数" + format);
        titleMap.put("punInVehicleTotal", "投放车辆总数" + format);
        titleMap.put("unPunInVehicleTotal", "未投放车辆总数" + format);
        titleMap.put("operationVehicleTotal", "可运营车辆总数" + format);
        titleMap.put("unOperationVehicleTotal", "不可运营车辆总数" + format);
        titleMap.put("hasOrderMachineCount", "产生订单车辆数");
        titleMap.put("vehicleAverageOrder", "车均订单数" + format);
        titleMap.put("vehicleAverageMoney", "车均订单收入" + "(元)" + format);
        titleMap.put("chargingTotal", "换电总次数");
        titleMap.put("moveTotal", "挪车总次数");
        titleMap.put("dispatchTotal", "调度总次数");
        titleMap.put("userTotal", "累计用户量" + format);
        titleMap.put("newUserTotal", "新增用户量");
        try {
            ExcelUtils.listToExcel(list, titleMap, "运营报表区域汇总", response);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     * 运营报表按天统计
     *
     * @param adAccountId 区域id
     * @param startTime   开始时间
     * @param endTime     结束时间
     * @return
     */
    @GetMapping("/groupByDay")
    public Result<List<OperationReportVO>> groupByDay(Integer adAccountId, Date startTime, Date endTime) {
        List<OperationReportVO> list = operationReportService.groupByDay(adAccountId, startTime, endTime);
        return Result.success(list);
    }

    /**
     * 运营报表按天统计导出
     *
     * @param adAccountIds 区域id列表
     * @param startTime   开始时间
     * @param endTime     结束时间
     * @return
     */
    @GetMapping("/groupByDayExport")
    public Result<Object> groupByDayExport(Integer adAccountId, Date startTime, Date endTime) {
        List<OperationReportVO> list = operationReportService.groupByDay(adAccountId, startTime, endTime);
        ExcelUtil<OperationReportVO> excelUtil = new ExcelUtil<>(OperationReportVO.class);
        //区域名字不会为空
        String areaName = list.get(0).getAreaName();
        return Result.success(excelUtil.exportExcel(list, areaName + "运营报表按天统计").getData());
    }
}
